package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.NPC;
import com.luckberry.murder.service.NPCService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiNPCController {

    @Resource
    private NPCService service;

    @ResponseBody
    @RequestMapping(value = "/npcs", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getNPCs() {
        return JSON.toJSONString(service.getNPCs());
    }

    @ResponseBody
    @RequestMapping(value = "/npc/{nid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getNPC(@PathVariable("nid") int nid) {
        return JSON.toJSONString(service.getNPC(nid));
    }

    @ResponseBody
    @RequestMapping(value = "/npcs/script", method = RequestMethod.GET)
    public String getNPCByScript(int sid) {
        return JSON.toJSONString(service.getNPCByScript(sid));
    }

    @ResponseBody
    @RequestMapping(value = "/npc", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addNPC(@RequestBody String json) {
        return JSONUtil.result(service.addNPC(JSON.parseObject(json, NPC.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/npc", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editNPC(@RequestBody String json) {
        return JSONUtil.result(service.editNPC(JSON.parseObject(json, NPC.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/npc/{nid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteNPC(@PathVariable("nid") int nid) {
        return JSONUtil.result(service.deleteNPC(nid));
    }
}
